package com.su27.web;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.PropertyFilter;

public class JSonUtil {
	public static DocReqBean parseJsonStr2Bean(String jsonStr) {
		JSONObject o = JSONObject.fromObject(jsonStr);

		DocReqBean bean = (DocReqBean) JSONObject.toBean(o, DocReqBean.class);

		return bean;

	}

	public static String parseBean2JsonStr(DocRespBean bean) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(DocRespBean.class,
				new JsonValueProcessor() {
					public Object processArrayValue(Object value,
							JsonConfig jsonConfig) {
						if (value == null) {
							return null;
						}
						return value;
					}

					public Object processObjectValue(String arg0, Object arg1,
							JsonConfig arg2) {
						return null;
					}

				});
		JSONObject jsonObject = JSONObject.fromObject(bean, jsonConfig);

		return jsonObject.toString();
	}

	public static void main(String[] args) {
		/*
		 * String jsonStr =
		 * "{\"method\":\"login\",\"username\":\"zhangsan\",\"password\":\"01D7F40760960E7BD9443513F22AB9AF\",\"imsi\":\"abcddd\"}"
		 * ; System.out.println(JSonUtil.parseJsonStr2Bean(jsonStr));
		 */

		DocRespBean respBean = new DocRespBean();
		respBean.setCount(10);
		respBean.setResult(true);
		respBean.setInfo("aa");
		Doc[] docs = new Doc[]{new Doc("1","浪潮之巅"),new Doc("2","Head First")};
		respBean.setDoc(docs);
		System.out.println(JSonUtil.parseBean2JsonStr(respBean));
	}
}
